881. 救生艇
为保证权益,题目请参考 881. 救生艇(From LeetCode).
解决方案1
Python
python
# 881. 救生艇
# https://leetcode-cn.com/problems/boats-to-save-people/
from typing import List
class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()
l = 0
r = len(people) - 1
count = 0
while l <= r:
if people[l] + people[r] <= limit:
l += 1
r -= 1
else:
r -= 1
count += 1
return count
if __name__ == "__main__":
solution = Solution()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24